package com.zfj.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zfj.entity.EduSubject;
import com.zfj.entity.excel.SubjectData;
import com.zfj.entity.subject.OneSubject;
import com.zfj.entity.subject.TwoSubject;
import com.zfj.listener.SubjectExcelListener;
import com.zfj.mapper.EduSubjectMapper;
import com.zfj.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author zfj
 * @since 2022-10-06
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            InputStream is = file.getInputStream();
            EasyExcel.read(is, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getOneTwoSubject() {
        //查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
        //查询所有二级分类  ne不等于
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperOne.ne("parent_id","0");
        List<EduSubject> TwoSubjectList = baseMapper.selectList(wrapperTwo);
        //封装一级分类
        List<OneSubject> finalSubject = new ArrayList<>();
        //查询出所有的一级分类list集合遍历，得到每一个一级分类对象，获取每一个一级分类值
        //封装到要求的list集合里面
        for (int i = 0; i < oneSubjectList.size(); i++) {
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
            //eduSubject获取出来放到OneSubject
            BeanUtils.copyProperties(eduSubject,oneSubject);

            finalSubject.add(oneSubject);
            List<TwoSubject> towFinalSubject = new ArrayList<>();
            for (int m = 0; m < TwoSubjectList.size(); m++) {
                //获取每个二级分类
                EduSubject tSubject = TwoSubjectList.get(m);
                //判断二级分类的parent——id和一级分类的id是否一致
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    //把tSubject值复制到TwoSubject中，放到twoFinalSubject中
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    towFinalSubject.add(twoSubject);
                }
            }
            //把一级下满的所有二级分类放到一级分类里面
            oneSubject.setChildren(towFinalSubject);
        }
        //查询二级分类
        return finalSubject;
    }
}
